﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Amazon_Book_Search.Models;

namespace Amazon_Book_Search.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            SearchPage[] pages = new SearchPage[] 
            {
                null, 
                new SearchPage { Items = new List<AmazonItem>(),
                                 NumPages = 0,
                                 NumResults = 0,
                                 PageNumber = 0 }, 
                null
            };

            return View("Search", pages); 
        }

        public ActionResult Search(string q = "", int p = 1)
        {
            SearchPage page_prev = null;
            if (p > 1) 
            {
                page_prev = new SearchPage(q, p - 1);
            }

            SearchPage page = new SearchPage(q, p);

            SearchPage page_next = null;
            if (p < page.NumPages)
            {
                page_next = new SearchPage(q, p + 1);
            }

            if (Request.IsAjaxRequest())
            {
                return PartialView("Search", new SearchPage[] { page_prev, page, page_next });
            }

            return View("Search", new SearchPage[] { page_prev, page, page_next });
        }

        public ActionResult SearchPartial(string q = "", int p = 1)
        {
            if (p < 1)
            {
                return new EmptyResult();
            }
            SearchPage page = new SearchPage(q, p);

            return PartialView("_SearchPartial", page);
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
